
import { CommonModel } from "../api/api-common/commonModel.js"
import { WorkModel } from "../api/api-work/workModel.js"
const workModel = new WorkModel()
const commonModel = new CommonModel()
// 根据机构id获取用户信息
const getUserInfo = async (orgId, that) => {
	if (orgId) {

		commonModel.getInfoApi({ orgId: orgId }).then(info => {
			// 权限存储
			wx.setStorageSync('permissions', info.permissions)
			// 用户信息存储
			wx.setStorageSync('userInfo', info.user);
			// 用户角色存储
			wx.setStorageSync('roles', info.roles);
			// tabbar存储
			const arr = changeTabbarList(info.list);
			wx.setStorageSync('tabbarList', arr);
			if (typeof that.getTabBar === 'function' && that.getTabBar()) {
				that.getTabBar().setData({
					list: arr
				})
			}
		})
	}
	else {
		if (typeof that.getTabBar === 'function' && that.getTabBar()) {
			that.getTabBar().setData({
				list: [{
					"pagePath": "/pages/tabbar/work/index",
					"text": "工作",
					"name": "work",
					"iconPath": "/images/tabbar/work.png",
					"selectedIconPath": "/images/tabbar/work-active.png"
				},
				{
					"pagePath": "/pages/tabbar/my/index",
					"text": "我的",
					"name": "my",
					"iconPath": "/images/tabbar/my.png",
					"selectedIconPath": "/images/tabbar/my-active.png"
				}]
			})
		}
	}
}

// 将返回的tabbarList格式化
const changeTabbarList = (tabbarList) => {
	const tabBarArr = [];
	tabbarList.forEach(item => {
		let obj = {
			"pagePath": item.path,
			"text": item.title,
			"name": item.routeName,
			"iconPath": item.icon,
			"selectedIconPath": item.component
		}
		tabBarArr.push(obj)
	})
	return tabBarArr;
}

// 根据routeName获取路由
const getRoutesByName = async (routeName) => {
	return await workModel.getTreeByRouteNameApi({ routeName: routeName })
}

// 将可调用的方法抛出让外面调用
module.exports = {
	getUserInfo,
	getRoutesByName,
	changeTabbarList
}